COMPILER=g++
LINKER=g++
OPTS = -c
PROTO_DIR = $(CURDIR)/../../proto
INCLUDE_DIR = $(CURDIR)/../../include
TPOOL = $(CURDIR)/../../lib/thrpool-0.8
INCLUDE_HEADER = -I$(CURDIR)/../../include/ -I$(CURDIR)/../../proto/ -I$(TPOOL)/src
FMAKE_BINARY = $(CURDIR)/../../bin/fmake
FCC_BINARY = $(CURDIR)/../../bin/fcc
LIB = $(CURDIR)/../../lib

PROTOBUFLIB = `pkg-config --cflags --libs protobuf` -Wl,-rpath,$(LIB)

LDFLAGS = -Wall -g -O0 $(PROTOBUFLIB)
PROTOC = ../../bin/protoc

OBJS = main.o simulator.o makefile_deps.o deps.o fast_build_helper.o server.o fast_build_client.o socket.o $(PROTO_DIR)/message.pb.o $(PROTO_DIR)/client_server.pb.o

all: $(FMAKE_BINARY) $(FCC_BINARY)

$(FMAKE_BINARY) : $(OBJS) $(PROTO_DIR)/message_proto
	pkg-config --cflags --libs protobuf
	$(LINKER) $(OBJS) -o $(FMAKE_BINARY) $(LDFLAGS) -lpthread -L$(TPOOL) -lthrpool

$(FCC_BINARY) : $(INCLUDE_DIR)/socket.h makefile_script.cpp socket.o
	pkg-config --cflags --libs protobuf
	g++ socket.o makefile_script.cpp $(PROTO_DIR)/message.pb.cc -o $(FCC_BINARY) $(LDFLAGS) $(INCLUDE_HEADER)

$(PROTO_DIR)/message_proto : $(PROTO_DIR)/message.proto
	$(PROTOC) -I=$(PROTO_DIR) --cpp_out=$(PROTO_DIR) $(PROTO_DIR)/message.proto
	pkg-config --cflags --libs protobuf
	$(COMPILER) $(OPTS) $(PROTO_DIR)/message.pb.cc $(LDFLAGS) -o $(PROTO_DIR)/message.pb.o -I=$(PROTO_DIR)
	@touch $(PROTO_DIR)/message_proto

$(PROTO_DIR)/client_server_proto : $(PROTO_DIR)/client_server.proto
	$(PROTOC) -I=$(PROTO_DIR) --cpp_out=$(PROTO_DIR) $(PROTO_DIR)/client_server.proto
	pkg-config --cflags --libs protobuf
	$(COMPILER) $(OPTS) $(PROTO_DIR)/client_server.pb.cc $(LDFLAGS) -o $(PROTO_DIR)/client_server.pb.o 
	@touch $(PROTO_DIR)/client_server_proto

main.o : main.cpp $(INCLUDE_DIR)/makefile_deps.h $(INCLUDE_DIR)/fast_build_helper.h $(INCLUDE_DIR)/server.h $(INCLUDE_DIR)/fast_build_client.h $(INCLUDE_DIR)/socket.h $(INCLUDE_DIR)/simulator.h
	$(COMPILER) $(OPTS) main.cpp $(INCLUDE_HEADER) -L$(TPOOL) -lthrpool



simulator.o : simulator.cpp $(INCLUDE_DIR)/makefile_deps.h $(INCLUDE_DIR)/deps.h $(INCLUDE_DIR)/simulator.h
	$(COMPILER) $(OPTS) simulator.cpp $(INCLUDE_HEADER)  -L$(TPOOL) -lthrpool

fast_build_client.o : $(INCLUDE_DIR)/fast_build_client.h $(INCLUDE_DIR)/server.h $(INCLUDE_DIR)/socket.h $(INCLUDE_DIR)/makefile_deps.h $(PROTO_DIR)/client_server_proto  $(PROTO_DIR)/message_proto fast_build_client.cpp
	$(COMPILER) $(OPTS) $(PROTO_DIR)/message.pb.cc $(PROTO_DIR)/client_server.pb.cc fast_build_client.cpp $(INCLUDE_HEADER)

server.o : $(INCLUDE_DIR)/server.h $(INCLUDE_DIR)/socket.h ../server.cpp
	$(COMPILER) $(OPTS) ../server.cpp $(INCLUDE_HEADER)

makefile_deps.o : $(INCLUDE_DIR)/deps.h $(INCLUDE_DIR)/makefile_deps.h $(INCLUDE_DIR)/fast_build_helper.h makefile_deps.cpp
	$(COMPILER) $(OPTS) makefile_deps.cpp $(INCLUDE_HEADER)

deps.o : $(INCLUDE_DIR)/deps.h deps.cpp $(INCLUDE_DIR)/observer.h
	$(COMPILER) $(OPTS) deps.cpp $(INCLUDE_HEADER) 

fast_build_helper.o : $(INCLUDE_DIR)/fast_build_helper.h fast_build_helper.cpp
	$(COMPILER) $(OPTS) fast_build_helper.cpp $(INCLUDE_HEADER)

socket.o : $(INCLUDE_DIR)/socket.h ../socket.cpp
	$(COMPILER) $(OPTS) ../socket.cpp $(INCLUDE_HEADER)

clean :
	rm -rf *.o $(FMAKE_BINARY) $(FCC_BINARY) $(PROTO_DIR)/message_proto $(PROTO_DIR)/*.o $(PROTO_DIR)/message.pb.h $(PROTO_DIR)/message.pb.cc $(PROTO_DIR)/client_server_proto $(PROTO_DIR)/client_server.pb.h $(PROTO_DIR)/client_server.pb.cc
